package com.csust.travel.controller;

import cn.hutool.core.util.StrUtil;
import com.csust.travel.common.Result;
import com.csust.travel.entity.NewUsers;
import com.csust.travel.entity.ResBean;
import com.csust.travel.entity.UserParam2;
import com.csust.travel.entity.UserParam3;
import com.csust.travel.mapper.NewUsersMapper;
import com.csust.travel.service.CodeService;
import com.csust.travel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    NewUsersMapper newUsersMapper;

    @Resource
    private UserService userService;

    @Resource
    private CodeService codeService;

    @GetMapping("/")
    public Result hello() {
        return Result.success("success");
    }

    @PostMapping("/login")
    public Result login(@RequestBody NewUsers newUsers) {
        if (StrUtil.isBlank(newUsers.getUsername()) || StrUtil.isBlank(newUsers.getPassword())) {
            return Result.error("数据输入不合法");
        }
        NewUsers newUsers1 = userService.login(newUsers);
        switch (newUsers1.getRole()) {
            case "用户":
                return new Result("200", "用户 " + newUsers.getUsername() + ", 登陆成功", newUsers1.getToken());
            case "管理员":
                return new Result("200", "管理员 " + newUsers.getUsername() + ", 登陆成功", newUsers1.getToken());
            case "超级管理员":
                return new Result("200", "超级管理员 " + newUsers.getUsername() + ", 登陆成功", newUsers1.getToken());
            default:
                return new Result("200", "访客 " + newUsers.getUsername() + ", 登陆成功", newUsers1.getToken());
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody NewUsers newUsers) {
        if (StrUtil.isBlank(newUsers.getUsername()) || StrUtil.isBlank(newUsers.getPassword())) {
            return Result.error("数据输入不合法");
        }
        if (newUsers.getUsername().length() > 10 || newUsers.getPassword().length() > 15) {
            return Result.error("数据输入不合法");
        }
        String email = newUsers.getEmail();
        String code = newUsers.getCode();
        System.out.println(code+" 0909");

        // 验证验证码
        if (!codeService.validateCode(email, code)) {
            return Result.error("验证码无效或已过期");
        }

        NewUsers login = userService.register(newUsers);
        return Result.success(login);
    }

    @PostMapping("/verify")
    public ResBean verify(@RequestBody NewUsers newUsers) {
        String email = newUsers.getEmail();
        String code = newUsers.getCode();
        System.out.println(code+" 0909");

        // 验证验证码
        if (!codeService.validateCode(email, code)) {
            return new ResBean(false,"验证码无效或已过期");
        }

        return new ResBean(true,"");
    }

    @PostMapping("/findPwd")
    public ResBean findThree( @RequestBody UserParam3 param) {

        String email = param.getEmail();
        String newPwd = param.getNewPwd();

        newUsersMapper.findPwd(newPwd,email);
        try {
            return new ResBean(true, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBean(false, null);
        }
    }
}
